package org.fly.jingwei.ext.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/6/2 11:22 下午
 * @since 1.0
 */
@Slf4j
public class PropUtils {
    private static final String KETTLE_JDBC_CONF_NAME = "kettle-jdbc.properties";
    private final Properties properties;

    private static class ConfigHolder {
        private static PropUtils instance(String propFileName) {
            return new PropUtils(propFileName);
        }
    }

    public static PropUtils getInstance(String propFileName) {
        return ConfigHolder.instance(propFileName);
    }

    public static PropUtils getKettleJdbcProp(){
        return getInstance(KETTLE_JDBC_CONF_NAME);
    }

    public String getValue(String key) {
        return properties.getProperty(key);
    }

    private PropUtils(String propFileName) {
        properties = new Properties();
        loadConfig(propFileName);
    }

    private void loadConfig(String propFileName) {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();

        try (InputStream is = loader.getResourceAsStream(propFileName)) {
            properties.load(is);
            log.info("配置文件：" + propFileName + "加载成功！");
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    private void loadConfigFromConfPath(){
        //TODO 从文件系统中，例如精卫的HOME目录中获取
    }
}
